package cn.b.training.baby_education_training.api;

import cn.b.training.baby_education_training.entity.BabyNote;
import cn.b.training.baby_education_training.service.BabyNoteService;
import cn.b.training.baby_education_training.service.dto.BabyNoteDto;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: baby_education_training
 * @description
 * @author: wuying
 * @create: 2022-12-04 13:44
 **/
@RestController
@RequestMapping("/note")
public class BabyNoteController {
    @Autowired
    private BabyNoteService babyNoteService;

    @PostMapping
    public ResponseEntity addNote(@RequestBody BabyNoteDto babyNoteDto){
        try {
            boolean b = babyNoteService.addNote(babyNoteDto);
            if (b){
                JSONObject result = new JSONObject();
                result.put("success","添加成功");
                return ResponseEntity.ok(result);
            }else {
                JSONObject error = new JSONObject();
                error.put("failed","添加失败");
                return ResponseEntity.status(400).body(error);
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error",e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @PutMapping
    public ResponseEntity updateNote(@RequestBody BabyNoteDto babyNoteDto){
        try {
            boolean b = babyNoteService.updateNoteInfo(babyNoteDto);
            if (b){
                JSONObject result = new JSONObject();
                result.put("success","修改成功");
                return ResponseEntity.ok(result);
            }else {
                JSONObject error = new JSONObject();
                error.put("failed","修改失败");
                return ResponseEntity.status(400).body(error);
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error",e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @GetMapping
    public ResponseEntity getNoteByBabyId(Long babyId) {
        try {
            List<BabyNote> babyNote = this.babyNoteService.getNoteByBabyId(babyId);
            return ResponseEntity.ok(babyNote);
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error", e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @GetMapping("/id")
    public ResponseEntity getNoteById(Long id) {
        try {
            BabyNote babyNote = this.babyNoteService.getNoteById(id);
            return ResponseEntity.ok(babyNote);
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error", e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @GetMapping("/title")
    public ResponseEntity getNoteByTitle(String title) {
        try {
            String newNoteTitle = "%"+title+"%";
            List<BabyNote> note = this.babyNoteService.getNoteByTitle(newNoteTitle);
            return ResponseEntity.ok(note);
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error", e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @DeleteMapping
    public ResponseEntity DeleteNoteById(Long id){
        try {
            boolean b = babyNoteService.deleteNoteById(id);
            if (b) {
                JSONObject result = new JSONObject();
                result.put("success", "删除成功");
                return ResponseEntity.ok(result);
            }else {
                JSONObject error = new JSONObject();
                error.put("failed","删除失败");
                return ResponseEntity.status(400).body(error);
            }
        }catch (Exception e){
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error",e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }
}
